package com.example.hyxydoctor.base;

import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import androidx.appcompat.app.AlertDialog;

import com.example.hyxydoctor.mine.LoginActivity;
import com.gyf.barlibrary.ImmersionBar;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

public abstract class MVPActivity<T extends BasePresenterOkgo> extends BaseActivity {

    protected T mPresenter;

    protected String TAG;

//    public static UserViewModel userViewModel;

    public static boolean isLogin;

    public static String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(getLayoutId());
        ImmersionBar.with(this).init();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mPresenter = getPresenter();
        mPresenter.attachView(this,this);
        TAG = this.getClass().getName();
        getLifecycle().addObserver(mPresenter);
//        LiveDataBus.get().with(1, User.class)
//                .observe(MyApplication.instance,user -> {
//                    if (user != null) {
//                        account = user.getAccount();
//                    } else {
//                        account = "";
//                    }
//                });
//        if (!TextUtils.isEmpty(MyApplication.getAccount()) && !TextUtils.isEmpty(MyApplication.getToken())) {
//            isLogin = true;
//        } else {
//            isLogin = false;
//        }
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ImmersionBar.with(this).destroy();
    }

    public void initEvent() {

    }

    public abstract int getLayoutId();

    public abstract T getPresenter();

    public abstract void initView();

    public abstract void initData();


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void otherLogin(OtherLoginEvent event) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("请先登录")
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        LoginActivity.start(MVPActivity.this);
                        AppManager.getInstance().finish2Login();
                    }
                }).show();

    }

}
